package in.miraclesirius.arunachaltimes;

import in.miraclesirius.arunachaltimes.slidingnavigation.NavDrawerItem;
import in.miraclesirius.arunachaltimes.slidingnavigation.NavDrawerListAdapter;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends Activity {

	public final static String RELATIVE_URL = "AT.Story.URL";
	public final static String DIV_ID = "AT.Story.divId";
	public final static String PAGE_TITLE = "AT.Story.pageTitle";

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	// private CharSequence mDrawerTitle;
	// private CharSequence mTitle;
	private ArrayList<NavDrawerItem> navDrawerItems;
	private NavDrawerListAdapter adapter;

	ProgressDialog mProgressDialog;
	final Context context = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// mTitle = mDrawerTitle = getTitle();
		// load slide menu items
		Resources res = getResources();
		TypedArray navMenu = res.obtainTypedArray(R.array.nav_drawer_items);

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

		navDrawerItems = new ArrayList<NavDrawerItem>();

		for (int i = 0; i < navMenu.length(); ++i) {
			String title = navMenu.getString(i);
			navDrawerItems.add(new NavDrawerItem(title));
		}
		navMenu.recycle();
		adapter = new NavDrawerListAdapter(getApplicationContext(),
				navDrawerItems);

		mDrawerList.setAdapter(adapter);
		// enabling action bar app icon and behaving it as toggle button
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, // nav menu toggle icon
				R.string.app_name, // nav drawer open - description for
									// accessibility
				R.string.app_name // nav drawer close - description for
									// accessibility
		) {
			public void onDrawerClosed(View view) {
				// getActionBar().setTitle(mTitle);
				// calling onPrepareOptionsMenu() to show action bar icons
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				// getActionBar().setTitle(mDrawerTitle);
				// calling onPrepareOptionsMenu() to hide action bar icons
				invalidateOptionsMenu();
			}
		};
		// mDrawerLayout.setDrawerListener(mDrawerToggle);

		if (savedInstanceState == null) {
			// on first time display view for first nav item
			displayView(0);
		}

		displayView(0);
		mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
	}

	private class SlideMenuClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			displayView(position);
		}
	}

	private void displayView(int position) {
		String title = navDrawerItems.get(position).getTitle();
		MainFragment fragment = new MainFragment();
		Bundle pigeon = new Bundle();
		switch (title) {
		case "Home":
			pigeon.putString(RELATIVE_URL, "");
			pigeon.putString(DIV_ID, "#txt_624");
			break;
		case "Editorial":
			pigeon.putString(RELATIVE_URL, "editorial.html");
			pigeon.putString(DIV_ID, "#txt_470");
			break;
		case "Features":
			pigeon.putString(RELATIVE_URL, "features.html");
			pigeon.putString(DIV_ID, "#txt_335");
			break;
		case "National":
			pigeon.putString(RELATIVE_URL, "national.html");
			pigeon.putString(DIV_ID, "#txt_548");
			break;
		case "News Imapct":
			pigeon.putString(RELATIVE_URL, "");
			pigeon.putString(DIV_ID, "#txt_521");
			break;
		case "North East":
			pigeon.putString(RELATIVE_URL, "north east.html");
			pigeon.putString(DIV_ID, "#txt_597");
			break;
		case "Readers Forum":
			pigeon.putString(RELATIVE_URL, "readers forum.html");
			pigeon.putString(DIV_ID, "#txt_622");
			break;
		case "Ring Side View":
			pigeon.putString(RELATIVE_URL, "ringside view.html");
			pigeon.putString(DIV_ID, "#txt_268");
			break;
		case "Sports":
			pigeon.putString(RELATIVE_URL, "sports.html");
			pigeon.putString(DIV_ID, "#txt_540");
			break;
		case "New version":
			Intent intent = new Intent(this, WordpressMainActivity.class);
			startActivity(intent);
			return;
		default:
			break;
		}

		pigeon.putString(PAGE_TITLE, title);
		fragment.setArguments(pigeon);
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.frame_container, fragment).commit();
		// update selected item and title, then close the drawer
		mDrawerList.setItemChecked(position, true);
		mDrawerList.setSelection(position);
		setTitle(navDrawerItems.get(position).getTitle());
		mDrawerLayout.closeDrawer(mDrawerList);
	}

	@Override
	public void setTitle(CharSequence title) {
		// mTitle = title;
		getActionBar().setTitle(title);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Pass any configuration change to the drawer toggls
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	//
	// getMenuInflater().inflate(R.menu.main, menu);
	// return true;
	// }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);

	}
}
